#!/bin/bash
#
# Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# generate fstab

DI_LUPIN=$(installer_get "DI_LUPIN")
DI_LOOP_ROOT_FILE=$(installer_get "DI_LOOP_ROOT_FILE")
DI_LOOP_SWAP_FILE=$(installer_get "DI_LOOP_SWAP_FILE")

#fstab for lupin
if [ x${DI_LUPIN} = xtrue ]; then
  echo "# <file system> <mount point>   <type>  <options>       <dump>  <pass>" > /target/etc/fstab
  echo "${DI_LOOP_ROOT_FILE} /               ext4    loop,errors=remount-ro 0       1" >> /target/etc/fstab
  echo "${DI_LOOP_SWAP_FILE} none            swap    loop,sw         0       0" >> /target/etc/fstab
  return 0
fi

readonly GENFSTAB="${HOOKS_DIR}/after_chroot/genfstab"
[ -f "${GENFSTAB}" ] || \
  error "require genfstab but it's not found. Abort!"
umount -v /target/media/cdrom
"${GENFSTAB}" -p -U /target > /target/etc/fstab

msg "Content of /etc/fstab"
cat /target/etc/fstab

# Add swap file option if it is set.
SWAP_FILE_REQUIRED=$(installer_get "DI_SWAP_FILE_REQUIRED")
SWAP_FILE_PATH=$(installer_get "partition_swap_file_path")
if [ x"${SWAP_FILE_REQUIRED}" = "xtrue" ]; then
  echo "${SWAP_FILE_PATH} none swap defaults 0 0" >> /target/etc/fstab
fi

chroot /target /usr/sbin/update-initramfs -u
